import {
	createRouter,
	createWebHistory
} from 'vue-router'
import Home from '../views/Home.vue'
import Login from '../views/auth/Login.vue'
import About from '../views/about/About.vue'
import Careers from '../views/about/Careers.vue'
import Contact from '../views/about/Contact.vue'
import FAQ from '../views/help/FAQ.vue'
import Guide from '../views/help/Guide.vue'
import Feedback from '../views/help/Feedback.vue'
import Partnership from '../views/partnership/Partnership.vue'
import Enterprise from '../views/enterprise/Enterprise.vue'
import Instructor from '../views/instructor/Instructor.vue'
import Profile from '../views/Profile.vue'
import Console from '../views/Console.vue'
import UserManagement from '../views/console/UserManagement.vue'
import ClassManagement from '../views/console/ClassManagement.vue'
import CourseManagement from '@/views/console/CourseManagement.vue'
import Resources from '../views/console/Resources.vue'
import Settings from '../views/console/Settings.vue'

const routes = [{
		path: '/login',
		name: 'Login',
		component: Login,
		meta: {
			title: '登录 - 智能直播互动教育平台',
			hideNavigation: true
		}
	},
	{
		path: '/',
		name: 'Home',
		component: Home,
		meta: {
			title: 'EduLiveLink - 智能直播互动教育平台'
		}
	},
	{
		path: '/console',
		name: 'Console',
		component: Console,
		meta: {
			title: '控制台 - 智能直播互动教育平台',
			requiresAuth: true
		}
	},
	{
		path: '/console/usermanagement',
		name: 'UserManagement',
		component: UserManagement,
		meta: {
			title: '用户管理 - 智能直播互动教育平台',
			requiresAuth: true
		}
	},
	{
		path: '/console/classmanagement',
		name: 'ClassManagement',
		component: ClassManagement,
		meta: {
			title: '班级管理 - 智能直播互动教育平台',
			requiresAuth: true
		}
	},
	 {
	        path: '/console/coursemanagement',
	        name: 'CourseManagement',
	        component: CourseManagement
	      },
	{
		path: '/console/resources',
		name: 'Resources',
		component: Resources,
		meta: {
			title: '资源管理 - 智能直播互动教育平台',
			requiresAuth: true
		}
	},
	{
		path: '/console/settings',
		name: 'Settings',
		component: Settings,
		meta: {
			title: '系统设置 - 智能直播互动教育平台',
			requiresAuth: true
		}
	},
	{
		path: '/home',
		redirect: '/'
	},
	{
		path: '/about',
		name: 'About',
		component: About,
		meta: {
			title: '关于我们 - 智能直播互动教育平台'
		}
	},
	{
		path: '/careers',
		name: 'Careers',
		component: Careers,
		meta: {
			title: '加入我们 - 智能直播互动教育平台'
		}
	},
	{
		path: '/contact',
		name: 'Contact',
		component: Contact,
		meta: {
			title: '联系我们 - 智能直播互动教育平台'
		}
	},
	{
		path: '/faq',
		name: 'FAQ',
		component: FAQ,
		meta: {
			title: '常见问题 - 智能直播互动教育平台'
		}
	},
	{
		path: '/profile',
		name: 'Profile',
		component: Profile,
		meta: {
			title: '个人中心 - 智能直播互动教育平台',
			requiresAuth: true
		}
	},
	{
		path: '/guide',
		name: 'Guide',
		component: Guide,
		meta: {
			title: '学习指南 - 智能直播互动教育平台'
		}
	},
	{
		path: '/feedback',
		name: 'Feedback',
		component: Feedback,
		meta: {
			title: '意见反馈 - 智能直播互动教育平台'
		}
	},
	{
		path: '/partnership',
		name: 'Partnership',
		component: Partnership,
		meta: {
			title: '机构合作 - 智能直播互动教育平台'
		}
	},
	{
		path: '/enterprise',
		name: 'Enterprise',
		component: Enterprise,
		meta: {
			title: '企业培训 - 智能直播互动教育平台'
		}
	},
	{
		path: '/instructor',
		name: 'Instructor',
		component: Instructor,
		meta: {
			title: '讲师招募 - 智能直播互动教育平台'
		}
	}
]

const router = createRouter({
	history: createWebHistory(),
	routes
})

// 路由守卫 - 设置页面标题和权限控制
router.beforeEach((to, from, next) => {
	// 设置页面标题
	if (to.meta.title) {
		document.title = to.meta.title
	}

	// 检查用户认证状态
	const isAuthenticated = localStorage.getItem('userToken') || sessionStorage.getItem('userInfo')

	console.log('路由守卫检查:', {
		to: to.path,
		isAuthenticated: !!isAuthenticated,
		requiresAuth: to.meta.requiresAuth
	})

	// 如果访问登录或注册页面，直接通过
	if (to.path === '/login' || to.path === '/register') {
		console.log('访问认证页面，直接通过')
		next()
		return
	}

	// 如果用户未认证，重定向到登录页
	if (!isAuthenticated) {
		console.log('未认证，重定向到登录页')
		next('/login')
		return
	}

	// 如果需要认证的页面且用户未登录
	if (to.meta.requiresAuth && !isAuthenticated) {
		console.log('需要认证但未登录，重定向到登录页')
		next('/login')
		return
	}

	console.log('路由守卫通过，继续导航')
	next()
})

export default router